package dviz.test.vo;

import java.util.HashMap;

import com.jme.math.Matrix3f;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.radakan.jme.mxml.MeshCloner;
import com.radakan.jme.mxml.anim.MeshAnimationController;

import dviz.tools.ModelRepository;
import dviz.visualSystem.impl.AnimationObject;


public class Heavy extends AnimationObject {

	Node heavyBodyHand;
	Node body;
	Node hand;
	MeshAnimationController bodyAnimation;
	MeshAnimationController handAnimation;
	final float NORMAL_SPEED = 0.3f;

	public Heavy(HashMap<String, Object> initVec) {
		super(initVec);
		ModelRepository modelRepository = ModelRepository.get();

		body = MeshCloner.cloneMesh((Node) modelRepository.getRepository().get(
				"heavyBodyMesh"));
		hand = MeshCloner.cloneMesh((Node) modelRepository.getRepository().get(
				"heavyHandMesh"));

		bodyAnimation = (MeshAnimationController) body.getControllers().get(0);
		handAnimation = (MeshAnimationController) hand.getControllers().get(0);

		// bodyAnimation.
		// System.out.println(bodyAnimation.isHardwareSkinning());
		// System.out
		// .println("GLShader() " + GLSLShaderObjectsState.isSupported());
		stand(new HashMap<String, Object>());
		// modelRepository.

		heavyBodyHand = new Node();
		heavyBodyHand.attachChild(body);
		heavyBodyHand.attachChild(hand);

		// bodyAnimation.setAnimation("stand");
		// handAnimation.setAnimation("stand");

		bodyAnimation.setSpeed(0.2f);
		handAnimation.setSpeed(0.2f);

		heavyBodyHand.setLocalScale(0.5f);

		// body.updateGeometricState(0, true);
		// body.updateRenderState();
		// hand.updateGeometricState(0, true);
		// hand.updateRenderState();

		// heavyBodyHand.setLocalScale(0.1f);
		getRenderNode().attachChild(heavyBodyHand);
		heavyBodyHand.updateGeometricState(0, true);
		heavyBodyHand.updateRenderState();
	}

	public void face(HashMap<String, Object> arg) {
		float x = (Float) arg.get("x");
		float y = (Float) arg.get("y");
		float len = (float) Math.sqrt(x * x + y * y);
		if (len < Float.MIN_VALUE)
			return;
		x /= len;
		y /= len;
		Matrix3f rot = new Matrix3f();
		Vector3f vY = new Vector3f(0, 1, 0);
		Vector3f vX = new Vector3f(y, 0, -x);
		Vector3f vZ = new Vector3f(x, 0, y);
		rot.fromAxes(vX, vY, vZ);
		getRenderNode().setLocalRotation(rot);
		// System.out.println("Heavy->Face() " + rot);
	}

	public void stand(HashMap<String, Object> arg) {
		if (!bodyAnimation.getActiveAnimation().equals("stand")) {
			bodyAnimation.setAnimation("stand");
			handAnimation.setAnimation("stand");
		}
	}

	public void walk(HashMap<String, Object> arg) {
		if (!bodyAnimation.getActiveAnimation().equals("run")) {
			bodyAnimation.setAnimation("run");
			handAnimation.setAnimation("run");
		}

		float vx = (Float) arg.get("x");
		float vy = (Float) arg.get("y");

		float speed = (float) Math.sqrt(vx * vx + vy * vy);

		bodyAnimation.setSpeed(speed * NORMAL_SPEED);
		handAnimation.setSpeed(speed * NORMAL_SPEED);
//		System.out.println("Animation->Heavy->Walk()");
	}

	// @Override
	// protected Collection<String> objectPropertyList() {
	// return null;
	// }

	public void setX(float x) {
		Vector3f v3f = getRenderNode().getLocalTranslation();
		v3f.setX(x);
		// getRenderNode().setLocalTranslation(v3f);
	}

	public void setY(float y) {
		Vector3f v3f = getRenderNode().getLocalTranslation();
		v3f.setZ(y);
		// getRenderNode().setLocalTranslation(v3f);
	}
}
